package com.zhangxin.threadcoreknowledge.threadobjectclasscommonmethods;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * 演示: 每秒输出当前的时间,被中断,观察结果
 */
public class Demo09SleepInterrupted implements Runnable{

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Demo09SleepInterrupted());
        thread.start();
        Thread.sleep(6500);
        thread.interrupt();

    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(new Date());
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                System.out.println("I was interrupted");
                e.printStackTrace();
            }
        }
    }
}
